package com.gz.mylife.app;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.adapter.BaseAdapterHelper;
import com.gz.mylife.adapter.QuickAdapter;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zl.smartmall.library.IGZAddress;
import com.zl.smartmall.library.listener.GetDictProvinceListener;
import com.zl.smartmall.library.po.DictProvinceInfo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class SelectCommunityActivity extends BaseActivity implements OnItemClickListener,OnClickListener,OnLoadingCoverRefreshListener{
	private PullToRefreshListView mListView;
	private QuickAdapter<DictProvinceInfo> mAdapter;
	private LoadingCover mloadCover;
	private List<DictProvinceInfo> mDataList;
	private MyListener mListener;
	private int currentAddress = -1;
	private MenuItem ensureItem;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selectcommunity);
		mDataList = new ArrayList<DictProvinceInfo>();
		setActionbarTitle("选择省份", true);
		initView();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.updata_menu, menu);
		ensureItem = menu.findItem(R.id.action_updata);
		MenuItemCompat.setShowAsAction(ensureItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		if(currentAddress == -1){
			setActionbarTitle("选择省份", false);
		}
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_updata:
			if(currentAddress == -1){
				Toast.makeText(this, "请先选择省份", Toast.LENGTH_SHORT).show();
				return true;
			}
			Intent intent = new Intent();
			intent.putExtra("obj", mDataList.get(currentAddress));
			setResult(109, intent);
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initView() {
		mloadCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mloadCover.setOnLoadingCoverRefreshListener(this);
		mListView = (PullToRefreshListView) findViewById(R.id.lv_community);
		mAdapter = bulidAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setMode(Mode.DISABLED);
		mListView.setOnItemClickListener(this);
		mListener = new MyListener(this);
		loadData();
	}

	private void loadData() {
		IGZAddress.getInstance().getDictProvince(mListener);
	}
	private QuickAdapter<DictProvinceInfo> bulidAdapter() {
		return new QuickAdapter<DictProvinceInfo>(getApplicationContext(),R.layout.selectcommunity_item,mDataList) {

			@Override
			protected void convert(BaseAdapterHelper helper, DictProvinceInfo item) {
				helper.setText(R.id.tv_name, item.getProvincename());
				ImageView ivselect = helper.getView(R.id.iv_select);
				if(item.getStatus() == 0){
					ivselect.setVisibility(View.GONE);
				}else{
					ivselect.setVisibility(View.VISIBLE);
				}
			}
		};
	}
	@Override
	public void onClick(View v) {
		
	}
	
	private static class MyListener extends GetDictProvinceListener{
		private SelectCommunityActivity mOuter;
		public MyListener(SelectCommunityActivity outer){
			this.mOuter = outer;
		}
		public void release(){
			new WeakReference<SelectCommunityActivity>(mOuter);
		}

		@Override
		public void onResponseSuccess(List<DictProvinceInfo> lists) {
			if(mOuter == null){
				return;
			}
			mOuter.mDataList.clear();
			mOuter.mDataList.addAll(lists);
			mOuter.mAdapter.clear();
			mOuter.mAdapter.addAll(mOuter.mDataList);
			mOuter.mloadCover.onRefreshSuccess();
		}

		@Override
		public void onNoMoreData() {
			if(mOuter == null){
				return;
			}
			mOuter.mloadCover.onRefreshFailed("暂无省份数据");
		}

		@Override
		public void onNoNetWork() {
			if(mOuter == null){
				return;
			}
			mOuter.mloadCover.onRefreshFailed();
			Toast.makeText(mOuter.getApplicationContext(), Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onResponseFailed() {
			if(mOuter == null){
				return;
			}
			mOuter.mloadCover.onRefreshFailed();
		}
		
	}

	@Override
	public void onLoadingCoverRefresh() {
		loadData();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(id>=0){
			if(id != currentAddress){
				if(currentAddress != -1){
					mDataList.get(currentAddress).setStatus(0);
				}else {
					ensureItem.setVisible(true);
				}
				currentAddress = (int)id;
				mDataList.get(currentAddress).setStatus(1);
			}
			mAdapter.clear();
			mAdapter.addAll(mDataList);
		}
	}

}
